package com.li.o2oSystem.web.shopadmin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.li.o2oSystem.dto.ProductCategoryExecution;
import com.li.o2oSystem.dto.Result;
import com.li.o2oSystem.entity.ProductCategory;
import com.li.o2oSystem.entity.Shop;
import com.li.o2oSystem.enums.ProductCategoryStateEnum;
import com.li.o2oSystem.exceptions.ProductCategoryOperationException;
import com.li.o2oSystem.service.ProductCategoryService;

@Controller
@RequestMapping(value="/shopadmin")
public class ProductCategoryManagementController {
	@Autowired
	private ProductCategoryService productCategoryService;
	/**
	 * 获取商品类别列表信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="getproductcategorylist",method=RequestMethod.GET)
	@ResponseBody
	private Result<List<ProductCategory>> getProductCategoryList(HttpServletRequest request){
		Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
		List<ProductCategory> list = null;
		if(currentShop != null && currentShop.getShopId() > 0){
			list = productCategoryService.getProductCategory(currentShop.getShopId());//传入shopId，查询该店铺的所有商品类别
			return new Result<List<ProductCategory>>(true,list);
		}else{
			ProductCategoryStateEnum ps = ProductCategoryStateEnum.INNER_ERROR;
			return new Result<List<ProductCategory>>(false,ps.getState(),ps.getStateInfo());
		}
	}
	/**
	 * 批量添加商品类别
	 * @param productCategoryList
	 * @param request
	 * @return
	 */
	@RequestMapping(value="addproductcategorys",method=RequestMethod.POST)
	@ResponseBody
	private Map<String,Object> addProductCategorys(@RequestBody List<ProductCategory> productCategoryList,HttpServletRequest request){
		Map<String,Object> modelMap = new HashMap<>();
		Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");//从session中获取当前操作的店铺
		for (ProductCategory pc : productCategoryList) {//遍历添加当前店铺id进商品类别中
			pc.setShopId(currentShop.getShopId());
			pc.setCreateTime(new Date());
		}
		if(productCategoryList != null && productCategoryList.size() > 0){//空值判断
			try{
				ProductCategoryExecution pe = productCategoryService.batchAddProductCategory(productCategoryList);
				if(pe.getState() == ProductCategoryStateEnum.SUCCESS.getState()){//判断商品分类操作的状态是否成功
					modelMap.put("success", true);
				}else{
					modelMap.put("success", false);
					modelMap.put("errMsg", pe.getStateInfo());
				}
			}catch(ProductCategoryOperationException e){
				modelMap.put("success", false);
				modelMap.put("errMsg", e.getMessage());
				return modelMap;
			}
		}
		else{
			modelMap.put("success", false);
			modelMap.put("success","请至少输入一个商品类别");
		}
		return modelMap;
	}
	/**
	 * 根据商品类别id和店铺id删除商品类别
	 * @param productCategoryId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="removeproductcategory",method=RequestMethod.POST)
	@ResponseBody
	private Map<String,Object> removeProductCategory(Long productCategoryId,HttpServletRequest request){
		Map<String,Object> modelMap = new HashMap<>();
		//判断控制，如果商品类别id不为空，并且大于0，则执行
		if(productCategoryId != null && productCategoryId > 0){
			try {
				Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");//从当前session中获取当前的店铺
				ProductCategoryExecution pe = productCategoryService.deleteProductCategory(productCategoryId, currentShop.getShopId());
				if(pe.getState() == ProductCategoryStateEnum.SUCCESS.getState()){
					modelMap.put("success", true);
				}else{
					modelMap.put("success", false);
					modelMap.put("errMsg", pe.getStateInfo());
				}
			} catch (ProductCategoryOperationException e) {
				modelMap.put("sucess", false);
				modelMap.put("errMsg", e.getMessage());
				return modelMap;
			}
		}else{
			modelMap.put("success", false);
			modelMap.put("errMsg", "请至少选择一个要删除的类别");
		}
		return modelMap;
	}
}
